<?
//*********************************************************************************************************
//***************************************** Check PHP Version *********************************************
//echo 'Current PHP version: ' . phpversion();
if (phpversion()< "4.1.0") {
	$_GET = $HTTP_GET_VARS;
	$_POST = $HTTP_POST_VARS;
	$_SERVER = $HTTP_SERVER_VARS;
}
//*********************************************************************************************************
//************************************** Get email config *************************************************
$emailConfigRecord = getRecord("tbl_config","code='adminEmail'");
$adminEmail = $emailConfigRecord['detail'];
//*********************************************************************************************************
//*********************************** Get currency unit config ********************************************
$currencyUnitConfigRecord = getRecord("tbl_config","code='currencyUnit'");
$currencyUnit = $currencyUnitConfigRecord['detail'];
//*********************************************************************************************************
//************************************** Public Key Interface *********************************************
function mo ($g, $l) {
	return $g - ($l * floor ($g/$l));
}
function powmod ($base, $exp, $modulus){
	$accum = 1;
	$i = 0;
	$basepow2 = $base;
	while (($exp >> $i)>0) {
		if ((($exp >> $i) & 1) == 1) {
			$accum = mo(($accum * $basepow2) , $modulus);
		}
		$basepow2 = mo(($basepow2 * $basepow2) , $modulus);
		$i++;
	}
	return $accum;
}
function PKI_Encrypt ($m, $e, $n){
	$asci = array ();
	for ($i=0; $i<strlen($m); $i+=3) {
		$tmpasci="1";
		for ($h=0; $h<3; $h++) {
			if ($i+$h <strlen($m)) {
				$tmpstr = ord (substr ($m, $i+$h, 1)) - 30;
				if (strlen($tmpstr) < 2) {
					$tmpstr ="0".$tmpstr;
				}
			} else {
				break;
			}
			$tmpasci .=$tmpstr;
		}
		array_push($asci, $tmpasci."1");
	}
	$coded = '';
	for ($k=0; $k< count ($asci); $k++) {
		$resultmod = powmod($asci[$k], $e, $n);
		$coded .= $resultmod." ";
	}
	return trim($coded);
}
function PKI_Decrypt ($c, $d, $n) {
	$decryptarray = split(" ", $c);
	for ($u=0; $u<count ($decryptarray); $u++) {
		if ($decryptarray[$u] == "") {
			array_splice($decryptarray, $u, 1);
		}
	}
	for ($u=0; $u< count($decryptarray); $u++) {
		$resultmod = powmod($decryptarray[$u], $d, $n);
		$deencrypt.= substr ($resultmod,1,strlen($resultmod)-2);
	}
	for ($u=0; $u<strlen($deencrypt); $u+=2) {
		$resultd .= chr(substr ($deencrypt, $u, 2) + 30);
	}
	return $resultd;
}
//************************************************************************************************************
function killInjection($str){//HAM NAY LOAI BO CAC LENH INJECTION
	$bad = array("\\","=",":");
	$good = str_replace($bad,"", $str);
	return $good;
}
//************************************************************************************************************
//************************************************* PAGING ***************************************************
function countPages($total, $n){
	if($total%$n==0) return (int)($total/$n);
	return (int)($total/$n)+1;
}
function createPage($total,$link,$nitem,$itemcurrent,$step=10){
	if($total<1){return false;}
	global $conn;
	$ret="";
	$param="";
	$pages = countPages($total,$nitem);
	if ($itemcurrent>0) $ret.='<a title="Trang đầu tiên" href="'.$link.'0" class="lslink">[&lt;&lt;]</a> ';
	if ($itemcurrent>1) $ret.='<a title="Về trang trước" href="'.$link.($itemcurrent-1).'" class="lslink">[&lt;]</a> ';
	$from=($itemcurrent-$step>0?$itemcurrent-$step:0);
	$to=($itemcurrent+$step<$pages?$itemcurrent+$step:$pages);
	for ($i=$from;$i<$to;$i++){
		if ($i!=$itemcurrent) $ret.='<a href="'.$link.$i.'" class="lslink">'.($i+1).'</a> ';
		else $ret.='<b>'.($i+1).'</b> ';
	}
	if (($itemcurrent<$pages-2) && ($pages>1)) $ret.='<a title="Tiếp theo" href="'.$link.($itemcurrent+1).'">[&gt;]</a> ';
	if ($itemcurrent<$pages-1) $ret.='<a title="Cuối cùng" href="'.$link.($pages-1).'">[&gt;&gt;]</a>'; 
	return $ret;
}
//************************************************************************************************************
//********************************************** SORT ********************************************************
function getLinkSort($order){
	$direction="";
	if ($_REQUEST['direction']==''||$_REQUEST['direction']!='0')
		$direction="0";
	else
		$direction="1";

	return "./?act=".$_REQUEST['act']."&cat=".$_REQUEST['cat']."&page=".$_REQUEST['page']."&sortby=".$order."&direction=".$direction;
}
//************************************************************************************************************
//************************************** file : upload *******************************************************
function getFileExtention($filename){  
    return strrchr($filename, ".");
}
function checkUpload($f,$ext="",$maxsize=0,$req=0){
	$fname=strtolower(basename($f['name']));
	$ftemp=$f["tmp_name"];
	$fsize=$f["size"];
	$fext=getFileExtention($fname);
	if($fsize==0){
		if ($req!=0) return "Bạn chưa chọn file !";
		return "";
	}else{
		if ($ext!="") if (strpos($ext, $fext)===false) 
			return "File không đúng định dạng : $fname";
		if ($maxsize>0) if ($fsize > $maxsize) 
			return "K&iacute;ch th&#432;&#7899;c h&igrave;nh ph&#7843;i nh&#7887; h&#417;n ".$maxsize." byte";
	}
	return "";
}
function makeUpload($f,$newfile){
	if (move_uploaded_file($f["tmp_name"], $newfile))	return $newfile;
	return false;
}
//************************************************************************************************************
function getRecord($table, $where='1=1'){
    global $conn;
    if ($table == '') return false;
	$result = mysql_query("select * from $table where $where limit 1",$conn);
	return @mysql_fetch_assoc($result);
}
function countRecord($table,$where=""){
	global $conn;
    if ($table=="") return false;
    if ($where=="") $where="1=1";
	$result = mysql_query("select count(*) as cnt from $table where $where",$conn);
	$row = @mysql_fetch_assoc($result);
	return $row['cnt'];
}
function dateFormat($dateField, $lang='vn'){
	if($dateField==''){return false;}
	$arrVN = array("Chủ nhật","Thứ Hai","Thứ Ba","Thứ tư","Thứ; năm","Thứ sáu","Thứ bảy");
	$arrEN = array("Sunday","Monday","Tueday","Wednesday","Thuday","Friday","Satuday");
	$date = strtotime($dateField);
	
	$arr = $lang=='vn'?$arrVN:$arrEN;
	
	return $arr[date('w',$date)].', '.date('d/m/Y',$date);
}
function dateFormat_NoDay($dateField, $lang='vn'){
	if($dateField==''){return false;}
	$arrVN = array("Chủ nhật","Thứ Hai","Thứ Ba","Thứ tư","Thứ; năm","Thứ sáu","Thứ bảy");
	$arrEN = array("Sunday","Monday","Tueday","Wednesday","Thuday","Friday","Satuday");
	$date = strtotime($dateField);
	
	//$arr = $lang=='vn'?$arrVN:$arrEN;
	
	return $arr[date('w',$date)].date('d/m/Y',$date);
}
function getArrayCategory($table, $catid="", $split="="){
    global $conn;
    $hide = "status=0";
    if (isset($_SESSION['log'])) $hide="1=1";
    $ret = array();
    if ($catid=="") $catid=2;
	$result = @mysql_query("select * from $table where $hide and parent=$catid",$conn);
	while($row=mysql_fetch_assoc($result)){
		$ret[] = array($row['id'],($catid==1?"":$split).$row['name']);
		$getsub = getArrayCategory($table, $row['id'], $split.$split);
		foreach ($getsub as $sub)
			$ret[]=array($sub[0],$sub[1]);
	}
	return $ret;
}

function getArrayCategoryChild($table, $catid="", $split="="){
    global $conn;
    $hide = "status=0";
    if (isset($_SESSION['log'])) $hide="1=1";
    $ret = array();
    if ($catid=="") $catid=3;
	$result = @mysql_query("select * from $table where $hide and parent=$catid",$conn);
	while($row=mysql_fetch_assoc($result)){
		$ret[] = array($row['id'],($catid==2?"":$split).$row['name']);
		$getsub = getArrayCategory($table, $row['id'], $split.$split);
		foreach ($getsub as $sub)
			$ret[]=array($sub[0],$sub[1]);
	}
	return $ret;
}

function getArrayNews($table, $catid="", $split="="){
    global $conn;
    $hide = "status=0";
    if (isset($_SESSION['log'])) $hide="1=1";
    $ret = array();
    if ($catid=="") $catid=2;
	$result = mysql_query("select * from $table where $hide and parent=$catid",$conn);
	while($row=mysql_fetch_assoc($result)){
		$ret[] = array($row['id'],($catid==1?"":$split).$row['name']);
		$getsub = getArrayCategory($table, $row['id'], $split.$split);
		foreach ($getsub as $sub)
			$ret[]=array($sub[0],$sub[1]);
	}
	return $ret;
}

function getArrayCombo($table, $valueField, $textField, $where=""){
	global $conn;
	$ret = array();
	$hide = "status=0";
	$where = $where!="" ? $where : "1=1";
	$result = mysql_query("select $valueField,$textField from $table where $hide and $where",$conn);
	while($row=mysql_fetch_assoc($result)){
		$ret[] = array($row[$valueField],$row[$textField]);
	}
	return $ret;
}
function getArray($query){
    global $conn;
    	$result = mysql_query($query,$conn);
	while($row=mysql_fetch_assoc($result)){
		$ret[] = array($row['id'],($catid==0?"":$split).$row['name']);
		$getsub = getArrayCategory($table, $row['id'], $split.'===');
		foreach ($getsub as $sub)
			$ret[]=array($sub[0],$sub[1]);
	}
	return $ret;
}

function isHaveChild($table, $id){
	global $conn;
	$result = mysql_query("select * from $table where parent=$id",$conn);
	$numRow = mysql_num_rows($result);
	return $numRow > 0 ? true : false;
}
//************************************************************************************************************
//****************************************** combo out HTML **************************************************
function comboLanguage($name, $langSelected, $class){
	global $arrLanguage;
	$name = $name != '' ? $name : 'cmbLang';
	$out = '';
	$out .= '<select size="1" name="'.$name.'" class="'.$class.'">';
	foreach ($arrLanguage as $lang){
		if ($lang[0] == $langSelected)
			$out .= '<option value="'.$lang[0].'" selected>'.$lang[1].'</option>';
		else
			$out .= '<option value="'.$lang[0].'">'.$lang[1].'</option>';
	}
	$out .= '</select>';
	return $out;
}

// $name            : name of combobox
// $arrSource  : function return array ; example : getListCategory(), getListNewsCategory()
// $index           : paramater selected
// $all             : $all==1 => show [Tat ca]
function comboCategory($name, $arrSource, $class, $index, $all){
	$name = $name != '' ? $name : 'cmbParent';
	if(!$arrSource){return false;}
	$out = '';
	$out .= '<select size="1" name="'.$name.'" class="'.$class.'">';
	$out .= $all==1 ? '<option value="">[Tất cả]</option>' : '';
	$cats = $arrSource;
	foreach ($cats as $cat){
		$selected = $cat[0] == $index ? 'selected' : '';
		$out .= '<option value="'.$cat[0].'" '.$selected.'>'.$cat[1].'</option>';
	}
	$out .= '</select>';
	return $out;
}


//************************************************************************************************************
//***************************************** SQL Query function ***********************************************
function insert($table,$fields_arr){
	global $conn;
	if(!$conn){return false;}
	$strfields="";
	$strvalues="";
	list($key, $val) = each($fields_arr);
	if(is_string($key)){
		$strfields = " ($key";
		$strvalues= $val;
		while(list($key, $val) = each($fields_arr)){
			$strfields.= ", $key";
			$strvalues.= ",".$val;
		}
			$strfields.=")";
	}else{
		$strvalues=$fields_arr[0];
		for($i=1;$i<(count($fields_arr));$i++){
			$strvalues .= ", $fields_arr[$i]";
		}
	}
	
	$query = "INSERT INTO $table $strfields VALUES ($strvalues)";
	//echo $query;
	return mysql_query($query, $conn);
}

function update($table,$fields_arr,$where) {
	global $conn;
	if (!$conn) { return false; }
	list($key, $val) = each($fields_arr);
	$strset=" $key = $val";
	while(list($key, $val) = each($fields_arr)){
		$strset .= ", $key = $val";
	}
	$query = "UPDATE $table SET $strset WHERE $where"; 
	$result = mysql_query($query, $conn);
	return !$result?false:true;
}

function delete_rows($table,$fields_arr,$where_ext="") {
	global $conn;
	if (!$conn) { return false; }
	if(count($fields_arr)>0){
		list($key, $val) = each($fields_arr);
		$strwhere=" $key = $val";
		while(list($key, $val) = each($fields_arr)){
			$strwhere .= "OR $key = $val";
		}
	}
	
	$query = "DELETE FROM $table WHERE $strwhere $where_ext";      
	#echo $query;#exit;
	$result = mysql_query($query, $conn);
	if (!$result) {return false;}
	return true;
}
//************************************************************************************************************
//************************************************ MAIL ******************************************************
function check_mail($email){
	if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) return false;
	return true;
}


//************************************************************************************************************
//************************************************************************************************************
?>